package com.example.andylau96.res.Main;

import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.andylau96.res.DataBase.UserDataBase;
import com.example.andylau96.res.R;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

/**
 * Created by AndyLau96 on 2016/12/20.
 */
public class pwdFragment extends Fragment implements View.OnClickListener
{
    private LinearLayout title;
    private ImageButton re;
    private Button ok;
    private EditText yuan,xin1,xin2;
    private SQLiteDatabase db;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.pwd,container,false);
        title= (LinearLayout) view.findViewById(R.id.pwd_title);
        re= (ImageButton) view.findViewById(R.id.pwd_title_return);
        ok= (Button) view.findViewById(R.id.pwd_title_ok);
        yuan= (EditText) view.findViewById(R.id.pwd_yuan);
        xin1= (EditText) view.findViewById(R.id.pwd_xin1);
        xin2= (EditText) view.findViewById(R.id.pwd_xin2);
        re.setOnClickListener(this);
        ok.setOnClickListener(this);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        MainActivity.now=this;
        String theme=null;
        File file=new File("/data/data/com.example.andylau96.res/files/color.txt");
        try {
            InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"UTF-8");
            BufferedReader br=new BufferedReader(reader);
            theme="";
            String temp=null;
            while((temp=br.readLine())!=null)
            {
                theme+=temp;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        title.setBackgroundColor(Color.parseColor(theme));
    }

    @Override
    public void onClick(View view) {
        int id=view.getId();
        if(id==R.id.pwd_title_return)
        {
            getFragmentManager().popBackStack();
        }
        if(id==R.id.pwd_title_ok)
        {
            String myuan=yuan.getText().toString();
            String mxin1=xin1.getText().toString();
            String mxin2=xin2.getText().toString();
            if(myuan.length()==0||mxin1.length()==0||mxin2.length()==0)
                Toast.makeText(getActivity(),"请输入完整信息",Toast.LENGTH_SHORT).show();
            else
            {
                db=new UserDataBase(getActivity().getApplicationContext()).getWritableDatabase();
                Cursor cr=db.rawQuery("select *from user where WorkId="+myuan,null);
                if(cr.getCount()==0)
                {
                    Toast.makeText(getActivity(),"原密码错误，请重试！",Toast.LENGTH_SHORT).show();
                }
                else{
                    if(!mxin2.equals(mxin1))
                    {
                        Toast.makeText(getActivity(),"两次输入密码不同，请重试！",Toast.LENGTH_SHORT).show();
                    }
                    else
                    {
                        db.execSQL("update user set PassWord='"+mxin1+"' where WorkId="+MainActivity.workid);
                        Toast.makeText(getActivity(),"修改成功，请重新登录",Toast.LENGTH_SHORT).show();
                        Intent intent=new Intent();
                        intent.setClass(getActivity(),Login.class);
                        startActivity(intent);
                        getActivity().finish();
                    }
                }
            }
        }
    }
}
